import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { BaseTableProps } from '@/components/BaseTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import { getFootprintReportList } from '@/apis/pcf/report/report'
import type { IReportParams } from '@/apis/pcf/report/model/reportModel'
import {
  type SearchOptionsType,
  ColumnSettingTypeEnum,
  type TableFieldsSettingType
} from '@/components/CPageTable'

export const useReportList = () => {
  // 搜索字段及其他
  const searchForm = reactive({
    modelName: '',
    standard: '',
    accountYear: ''
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '产品编码或名称',
      field: 'modelName',
      type: 'input'
    },
    {
      name: '核算标准',
      field: 'standard',
      type: 'select',
      fixedOption: 'StandardOptions',
      style: 'width: 155px'
    },
    {
      name: '核算年份',
      field: 'accountYear',
      type: 'datePicker',
      dateType: 'year',
      format: 'YYYY'
    }
  ])

  // 表格配置
  const tableOptions: BaseTableProps = {
    loadData,
    selection: false
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '产品信息',
      field: 'modelInfo',
      minWidth: 200,
      slot: 'modelInfo',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '所属企业',
      field: 'enterpriseName',
      minWidth: 160,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '核算标准',
      field: 'standard',
      width: 200,
      slot: 'standard',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '核算年份',
      field: 'accountYear',
      width: 100,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '碳足迹',
      field: 'unitResult',
      slot: 'unitResult',
      minWidth: 200,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 230,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter)
    return getFootprintReportList(params)
  }
  const handleParams = () => {
    let params: IReportParams = {
      standard: searchForm.standard,
      accountYear: searchForm.accountYear,
      modelName: searchForm.modelName
    }
    params = removeUndefinedParams(params)
    return params
  }

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
